//package com.dragon.user_service.config;
//
//import com.alibaba.druid.pool.DruidDataSource;
//import io.seata.rm.datasource.DataSourceProxy;
//import org.apache.shardingsphere.driver.api.ShardingSphereDataSourceFactory;
//import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
//import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration;
//import org.apache.shardingsphere.sharding.api.config.strategy.sharding.StandardShardingStrategyConfiguration;
//import org.springframework.beans.factory.annotation.Qualifier;
//import org.springframework.boot.context.properties.ConfigurationProperties;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.context.annotation.Primary;
//
//import javax.sql.DataSource;
//import java.sql.SQLException;
//import java.util.Collections;
//import java.util.HashMap;
//import java.util.Map;
//import java.util.Properties;
//
//@Configuration
//public class SeataShardingConfig {
//
//    // 原始数据源（Druid）
//    @Bean
//    @ConfigurationProperties(prefix = "spring.datasource")
//    public DataSource druidDataSource() {
//        return new DruidDataSource();
//    }
//
//    // Seata代理原始数据源
//    @Bean
//    public DataSourceProxy dataSourceProxy(DataSource dataSource) {
//        return new DataSourceProxy(dataSource);
//    }
//
//    // ShardingSphere代理Seata代理后的数据源
//    @Bean
//    @Primary // 标记为主数据源
//    public DataSource shardingSphereDataSource(
//        @Qualifier("dataSourceProxy") DataSourceProxy dataSourceProxy
//    ) throws SQLException {
//        Map<String, DataSource> dsMap = new HashMap<>();
//        dsMap.put("user_permission_log", dataSourceProxy);
//
//        // 动态加载分片规则（原yml配置转代码）
//        ShardingRuleConfiguration shardingRule = new ShardingRuleConfiguration();
//        ShardingTableRuleConfiguration tableRule = new ShardingTableRuleConfiguration(
//            "user",
//            "user_permission_log.user_$->{0..4}"
//        );
//        tableRule.setTableShardingStrategy(new StandardShardingStrategyConfiguration(
//            "user_id",
//            "user_table_sharding"
//        ));
//        shardingRule.getTables().add(tableRule);
//
//        return ShardingSphereDataSourceFactory.createDataSource(
//            dsMap,
//                Collections.singleton(shardingRule),
//            new Properties()
//        );
//    }
//}